package com.feel.controller;

import cn.hutool.core.util.StrUtil;
import com.feel.pojo.Part;
import com.feel.pojo.RespBean;
import com.feel.service.PartService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 刘永钦
 * @Date: 2024-09-03-20:33
 * @Description:
 */
@RestController
@RequestMapping("/admin/part")
public class PartController {
    @Autowired
    private PartService partService;

    /**
     * 分页获得数据
     */
    @GetMapping
    RespBean getAll(Integer page, String name, String supplier) {
        if (StrUtil.hasEmpty(name)) {
            name = null;
        }
        if (StrUtil.hasEmpty(supplier)) {
            supplier = null;
        }
        Part part = new Part();
        part.setPartName(name);
        part.setSupplier(supplier);
        PageInfo<Part> list = partService.getAll(page, part);
        return RespBean.ok("查询成功", list);
    }
    /**
     * 获得配件列表
     */
    @GetMapping("/list")
    RespBean getList() {
        List<Part> list = partService.getList();
        return  RespBean.ok("查询成功", list);
    }
    /**
     * 更新配件
     */
    @PutMapping
    RespBean updateInfo(@RequestBody Part part) {
        Boolean b = partService.updateInfo(part);
        return b ? RespBean.ok("更新成功") : RespBean.error("更新失败");
    }

    /**
     * 新增配件
     */
    @PostMapping
    RespBean addInfo(@RequestBody Part part) {
        Boolean b = partService.addInfo(part);
        return b ? RespBean.ok("新增成功") : RespBean.error("新增失败");
    }

    /**
     * 通过id查询
     */
    @GetMapping("/{id}")
    RespBean getByid(@PathVariable("id") Integer id) {
        Part part = partService.getById(id);
        return RespBean.ok("查询成功", part);
    }
}
